package com.huawei.java.main.server;

import java.util.*;

/**
 * @author dengguoli
 * @date 2022/3/16
 * @since jdk1.8
 */
public class ServerClientQos {
    // qos限制
    Integer qos;

    // server K 与client V 对应,可以供应
    Map<String, Set<String>> serverClientQosList;

    String[] clientName;

    String[] serverName;

    //client K 与 server V 对应，可以供应
    Map<String, Set<String>> clientServerQosList;

    public ServerClientQos (List<String[]> csvFile, Integer qos) {
        this.qos = qos;
        this.clientName = Arrays.copyOfRange(csvFile.get(0), 1, csvFile.get(0).length);
        this.serverName = serverNameSetter(csvFile);
        this.serverClientQosList = parseServerQos(csvFile);
        this.clientServerQosList = parseClientQos(csvFile);
    }

    private String[] serverNameSetter(List<String[]> csvFile) {
        String[] serverName = new String[csvFile.size()-1];
        for (int serverNum = 0; serverNum < csvFile.size(); serverNum ++) {
            if (serverNum == 0) {
                continue;
            }
            serverName[serverNum-1] = csvFile.get(serverNum)[0];
        }
        return serverName;
    }


    private Map<String, Set<String>> parseClientQos(List<String[]> csvFile) {

        HashMap<String, Set<String>> result = new HashMap<>(csvFile.get(0).length - 1);
        for (int clientNum = 0; clientNum < csvFile.get(0).length; clientNum++) {
            if (clientNum == 0) {
                continue;
            }
            Set<String> servers = new HashSet<>();
            for (int serverNum = 0; serverNum < csvFile.size(); serverNum ++) {
                if (serverNum == 0) {
                    continue;
                }
                if (Integer.parseInt(csvFile.get(serverNum)[clientNum]) <= qos) {
                    servers.add(serverName[serverNum-1]);
                }
            }
            result.put(clientName[clientNum-1], servers);
        }
        return result;
    }

    private Map<String, Set<String>> parseServerQos(List<String[]> csvFile) {
        HashMap<String, Set<String>> result = new HashMap<>(csvFile.size() - 1);
        for (int serverNum = 0; serverNum < csvFile.size(); serverNum++) {
            if (serverNum == 0) {
                continue;
            }
            Set<String> clients = new HashSet<>();
            result.put(serverName[serverNum - 1], clients);
            for (int clientNum = 0; clientNum < csvFile.get(0).length; clientNum ++) {
                if (clientNum == 0) {
                    continue;
                }
                Set<String> strings = result.get(serverName[serverNum - 1]);
                if (Integer.parseInt(csvFile.get(serverNum)[clientNum]) <= qos) {
                    strings.add(clientName[clientNum-1]);
                }
            }
        }
        return result;
    }

    public Integer getQos() {
        return qos;
    }

    public void setQos(Integer qos) {
        this.qos = qos;
    }

    public Map<String, Set<String>> getServerClientQosList() {
        return serverClientQosList;
    }

    public void setServerClientQosList(Map<String, Set<String>> serverClientQosList) {
        this.serverClientQosList = serverClientQosList;
    }

    public String[] getClientName() {
        return clientName;
    }

    public void setClientName(String[] clientName) {
        this.clientName = clientName;
    }

    public String[] getServerName() {
        return serverName;
    }

    public void setServerName(String[] serverName) {
        this.serverName = serverName;
    }

    public Map<String, Set<String>> getClientServerQosList() {
        return clientServerQosList;
    }

    public void setClientServerQosList(Map<String, Set<String>> clientServerQosList) {
        this.clientServerQosList = clientServerQosList;
    }
}
